﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using SilverlightSdms.VisitsService;
using System.Globalization;

namespace SilverlightSdms.Views
{
    public partial class Doctor : Page
    {
        Button[] m_btnsMonths = new Button[12];

        public Doctor()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void tabItem1_Loaded(object sender, RoutedEventArgs e)
        {
            //this.facilities1.LoadAllFacilitiesData();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            // Do not load your data at design time.
            // if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            // {
            // 	//Load your data here and assign the result to the CollectionViewSource.
            // 	System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["Resource Key for CollectionViewSource"];
            // 	myCollectionViewSource.Source = your data
            // }
        }

        private void tabItem3_Loaded(object sender, RoutedEventArgs e)
        {
            this.userControlControlPayCheckSummary.Init(((App)Application.Current).UserId, new DateTime(2012, 1, 1));
            string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;

            StackPanelPayCheckSummaryMonths.Children.Clear();
            for (int i = months.Length - 2; i >= 0 ; i--)
            {
                m_btnsMonths[i] = new Button();
                m_btnsMonths[i].HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                m_btnsMonths[i].VerticalContentAlignment = System.Windows.VerticalAlignment.Center;            
                //m_btnsMonths[i].Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xE5, 0xE5, 0xE5));  // alpha, red, green, blue
                m_btnsMonths[i].Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0xA8, 0xFF));
                m_btnsMonths[i].DataContext = new DateTime(2012, i + 1, 1);
                m_btnsMonths[i].Content = months[i];
                m_btnsMonths[i].Margin = new Thickness(0, 0, 0, 4);
                m_btnsMonths[i].Width = 85;
                m_btnsMonths[i].Click += new RoutedEventHandler(Doctor_Click);
                StackPanelPayCheckSummaryMonths.Children.Add(m_btnsMonths[i]);
            }

        }

        void Doctor_Click(object sender, RoutedEventArgs e)
        {
            var time =  (DateTime)((sender as Button).DataContext);
            this.userControlControlPayCheckSummary.Init(((App)Application.Current).UserId, time);
        }

        private void visitDataGrid_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
        {
            var visit = (Visit)e.Row.DataContext;
            var client = new VisitsService.VisitsServiceClient();

            client.UpdateCompleted += (s, ea) =>
            {
                if (ea.Error == null)
                {
                    MessageBox.Show("Visit updated succesfully");
                }
                else
                {
                    MessageBox.Show("Failed update visit");
                }
            };

            client.UpdateAsync(visit);
        }

    }
}
